package cn.itcast.zt.config;

import cn.itcast.zt.sub.MessageChatReceiver;
import cn.itcast.zt.sub.MessagePlayReceiver;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author zhangtian105
 * @ClassName RedisConfig.java
 * @projectName Redis
 * @Description Redis配置 多订阅通道
 * @createTime 2020年04月09日 11:09:00
 */
@Configuration
public class RedisConfig {

    @Bean
    public Jackson2JsonRedisSerializer<Object> serializer() {
        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();

        // 指定要序列化的域，field,get和set,以及修饰符范围，ANY是都有包括private和public
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        // 指定序列化输入的类型，类必须是非final修饰的，final修饰的类，比如String,Integer等会跑出异常
        objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        return jackson2JsonRedisSerializer;
    }

    // 注册redis与订阅通道
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter messageChatListenerAdapter,
                                                   MessageListenerAdapter messagePlayListenerAdapter,
                                                   Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer() ;
        container.setConnectionFactory(connectionFactory);

        // 订阅一个叫chat的通道
        container.addMessageListener(messageChatListenerAdapter, new PatternTopic("chat"));
        // 订阅一个叫paly的通道
        container.addMessageListener(messagePlayListenerAdapter, new PatternTopic("play"));

        // 序列化对象（特别注意：发布的时候需要设置序列化；订阅方也需要设置序列化）
        container.setTopicSerializer(jackson2JsonRedisSerializer);
        return container ;
    }

    // 消息监听器适配器，绑定消息处理器，利用反射技术调用消息处理器的业务方法
    @Bean
    public MessageListenerAdapter messageChatListenerAdapter(MessageChatReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveChatMessage");
    }

    @Bean
    public MessageListenerAdapter messagePlayListenerAdapter(MessagePlayReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receivePlayMessage");
    }

    // 读取内容的template
    @Bean
    public RedisTemplate<String, Object> template(RedisConnectionFactory connectionFactory,
                                        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer) {

        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
